﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using nutricion;

namespace NutriFast
{
    public partial class DietaUpdatePacient : Form
    {
        DietaSelPaciente DSP;
        public int currentpacient=0;
        bool insertado=false;
        public DietaUpdatePacient(DietaSelPaciente dsp)
        {
            InitializeComponent();
            DSP = dsp;
            this.nmeals.SelectedIndex = 0;
            this.CHO.SelectedIndex = 0;
            this.proteinas.SelectedIndex = 5;
            this.lipidos.SelectedIndex = 15;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            UsuarioUpdate uu = new UsuarioUpdate();
            uu.selected = currentpacient;
            uu.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DSP.Show();
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dbVic.actualizarPeso(this.currentpacient, (float)this.he.Value);
            Patient paciente = dbVic.crearPaciente(currentpacient);
            if (insertado == true) {
                Dieta dieta = new Dieta(this.currentpacient, float.Parse("" + this.nmeals.SelectedItem), (float)this.he.Value, float.Parse("" + proteinas.SelectedItem), float.Parse("" + CHO.SelectedItem), float.Parse("" + lipidos.SelectedItem), (float)Formulas.kilocalorias(paciente.Sex, paciente.Cw, paciente.He, paciente.Bd), 0);
                if (dbVic.actualizarDieta(dieta) && dbVic.actualizarPeso(this.currentpacient, (float)this.he.Value))
                {
                    insertado = true;
                    //Message
                    //MessageBox.Show(this, "El paciente se ha asociado a una nueva dieta", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    DietaSelComida Ds = new DietaSelComida(currentpacient,int.Parse("" + nmeals.SelectedItem), this);
                    Ds.MdiParent = this.MdiParent;
                    this.Hide();
                    Ds.Show();
                }
                else
                {
                    MessageBox.Show(this, "Error al asociar Paciente", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }//Fin del else
            
            }

            else
            {

                if ((int.Parse("" + this.CHO.SelectedItem) + int.Parse("" + this.proteinas.SelectedItem) + int.Parse("" + this.lipidos.SelectedItem) > 100) || (int.Parse("" + this.CHO.SelectedItem) + int.Parse("" + this.proteinas.SelectedItem) + int.Parse("" + this.lipidos.SelectedItem)) < 100)
                    MessageBox.Show(this, "La suma de los porcentajes de macrominerales \n Son diferentes de 100%", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {

                    //CrearObjeto
                    Dieta dieta = new Dieta(this.currentpacient, float.Parse("" + this.nmeals.SelectedItem), (float)this.he.Value, float.Parse("" + proteinas.SelectedItem), float.Parse("" + CHO.SelectedItem), float.Parse("" + lipidos.SelectedItem), (float)Formulas.kilocalorias(paciente.Sex, paciente.Cw, paciente.He, paciente.Bd), 0);
                    //Mandar llamarlo
                    //Insertar Paciente

                    if (dbVic.actualizarPeso(this.currentpacient, (float)this.he.Value) && dbVic.insertarDieta(dieta))
                    {
                        insertado = true;
                        //Message
                        //MessageBox.Show(this, "El paciente se ha asociado a una dieta", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        DietaSelComida Ds = new DietaSelComida(currentpacient,int.Parse("" + nmeals.SelectedItem),this);
                        Ds.MdiParent = this.MdiParent;
                        this.Hide();
                            Ds.Show();
                    }
                    else
                    {
                        MessageBox.Show(this, "Error al asociar Paciente", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }//Fin del else
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {

            if((int.Parse(""+this.CHO.SelectedItem)+int.Parse(""+this.proteinas.SelectedItem)+int.Parse(""+this.lipidos.SelectedItem)>100) || (int.Parse(""+this.CHO.SelectedItem)+int.Parse(""+this.proteinas.SelectedItem)+int.Parse(""+this.lipidos.SelectedItem))<100)
                MessageBox.Show(this, "La suma de los porcentajes de macrominerales \n Son diferentes de 100%", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
            
            Historial his = new Historial(currentpacient,float.Parse(""+this.CHO.SelectedItem),float.Parse(""+this.lipidos.SelectedItem),float.Parse(""+this.proteinas.SelectedItem));
            his.name.Text = this.name.Text;
                his.MdiParent=this.MdiParent;
            his.Show();
            }
        }

        private void DietaUpdatePacient_Load(object sender, EventArgs e)
        {
            try
            {
                //OpenFileDialog open = new OpenFileDialog();
                //open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                
                //if (open.ShowDialog() == DialogResult.OK)
               // {
                    string filename = "../../Fotos/" + currentpacient + ".jpg";
                    pictureBox1.Image = new Bitmap(filename);
                    //Console.WriteLine(open.FileName);
                //}
            }
            catch (Exception)
            {
                pictureBox1.Image= NutriFast.Properties.Resources.defaultUser;
            }
        }

        private void DietaUpdatePacient_Click(object sender, EventArgs e)
        {
            try
            {
                //OpenFileDialog open = new OpenFileDialog();
                //open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

                //if (open.ShowDialog() == DialogResult.OK)
                // {
                string filename = "../../Fotos/" + currentpacient + ".jpg";
                pictureBox1.Image = new Bitmap(filename);
                //Console.WriteLine(open.FileName);
                //}
            }
            catch (Exception)
            {
                pictureBox1.Image = NutriFast.Properties.Resources.defaultUser;
            }
        }

        private void DietaUpdatePacient_Validated(object sender, EventArgs e)
        {
            try
            {
                //OpenFileDialog open = new OpenFileDialog();
                //open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

                //if (open.ShowDialog() == DialogResult.OK)
                // {
                string filename = "../../Fotos/" + currentpacient + ".jpg";
                pictureBox1.Image = new Bitmap(filename);
                //Console.WriteLine(open.FileName);
                //}
            }
            catch (Exception)
            {
                pictureBox1.Image = NutriFast.Properties.Resources.defaultUser;
            }
        }

        private void DietaUpdatePacient_FormClosing(object sender, FormClosingEventArgs e)
        {
            //webcam.Stop();
        }

        private void DietaUpdatePacient_VisibleChanged(object sender, EventArgs e)
        {
            try
            {
                string filename = "../../Fotos/" + currentpacient + ".jpg";
                pictureBox1.Image = new Bitmap(filename);
            }
            catch (Exception)
            {
                pictureBox1.Image = NutriFast.Properties.Resources.defaultUser;
            }
        }

    }
}
